<?php
namespace Admin\Controller;
use Think\Controller;
//use Admin\Controller\BaseController;
class GroupsController extends BaseController {
   // private $model;
    public function _initialize(){
        $this->tbl='groups';
      //  $this->model=D($this->tbl);
        parent::_initialize();


    }

    public function index(){
     
    //  $this->model=M('groups');
	 /* 	$data=$this->model->select();
		$this->assign('list',$data);
		//dump($data);
		$this->display();*/
       parent::index($this->tbl);
    }
	
    public function view(){
		$id=I('get.id');

	}
    public function mod(){
    /*    $id = I('get.id');
        //dump($id);exit;//调试
        //$this->model=M('user');
        $one = $this->model->find($id);
        //dump($one);exit;
        $this->assign('one',$one);
        $this->display();*/
      parent::mod($this->tbl);    
    }

        public function check(){
       $id = I('get.id');
        //dump($id);exit;//调试
        $this->model=M('groups');
        $one = $this->model->find($id);
        //dump($one);exit;
        $this->assign('one',$one);
        $this->display();
     /*  parent::check($this->tbl);   */ 
    }





    public function update(){ 
     //   $this->model=D('groups');
     $this->model=M('groups');
         $data = I('post.');
        //$data['hobby']=implode(',', $data['hobby']);
        //dump($data);exit;
     
           if(!empty($_FILES['ydq_logo']['name'])){
        /*   $photo=$this->model->where ('id'.$data['ydq_id'])->getField('ydq_logo');
            if(is_file(filename)){
                unlink();
            }
            if (is_file(filename)) {
                unlink();
                # code...
            }*/
            $data['ydq_logo']=$this->uploadfile('ydq_logo');
        }
        if($this->model->create($data)){
            $this->model->save();//用save必须有id 
            $this->success('更新成功！',U('Groups/index'));
        }
        else{
            $this->error('更新失败！');

        }
       /*   parent::update($this->tbl,'ydq_logo');*/
    }

    public function form(){
       //test();
    //	$this->display();
        parent::form($this->tbl);  
    }

    
    public function search(){
    /*	$key=I('post.search_key');
    	$sql="select * from ydq_user where ydq_username like '%".$key."%'";
    	$data=M()->query($sql);
    	//M()->ex
    	$this->assign('user',$data);
    	$this->display();*/
        parent::search($this->tbl,'ydq_groupname');



    }
 /*   public function add(){
        $data = I('post.');
       // dump($_FILES);
        $data['ydq_photo']=$this->uploadfile('ydq_photo');
        $data['ydw_photo']='';
        if(!empty($_FILES['ydq_photo']['name'])){
            $data['ydq_photo']=$this->uploadfile('ydq_photo');
        }
       // dump($data);exit;
        //
        //$data['whr_username']='';
        //$data['whr_username']=I['post.username'];伪装
        $this->model = D('user');
        //dump($this->model);exit;//D方法做验证
        if ($this->model->create($data)) {
            $this->model->add();
            $this->success('创建成功！',U('admin.php/index/index'));
        }else{
            $this->error('创建失败！'.$this->model->getError());
        }
    }*/

        public function add(){
      $data = I('post.');
       // dump($_FILES);
        $data['ydq_logo']=$this->uploadfile('ydq_logo');
     //  dump($data);exit;
        //
        //$data['whr_username']='';
        //$data['whr_username']=I['post.username'];伪装
        $this->model = D('groups');
        //dump($this->model);exit;//D方法做验证
        if ($this->model->create($data)) {
            $this->model->add();
            $this->success('创建成功！',U('groups/index'));
        }else{
            $this->error('创建失败！'.$this->model->getError());
          //  parent::add($this->tbl,'ydq_logo');
        }
    

}



        public function addchengyuan(){
     //dump($mfield);exit;
        $this->model=D('members');
        $data=I('post.'); 
        $ydq_id=$data['ydq_id'];
        $ydq_stunum= explode(",",$data['ydq_stunum']);
        for($i=0; $i <count($ydq_stunum); $i++){ 
            $sql="select ydq_realname 
            from ydq_users 
            where ydq_stunum=".$ydq_stunum[$i];
            $array=M()->query($sql);
            $str[$i]=array(
            'ydq_id'=>$ydq_id,
           // 'ydq_cid'=>$cq_cid,
            'ydq_stunum'=>$ydq_stunum[$i],
            'ydq_realname'=>$array[0]['ydq_realname']);
            
        }    
        for($i=0; $i <count($str); $i++){ 
            $insert[$i]=$this->model->create($str[$i]);
            if ($insert[$i]) {
               $this->model->add($insert[$i]);
               //dump($insert[$i]);exit;
            }
            else{
            $this->error($this->model->getError());
            }
        } 
        
        $this->success('添加成功！',U('Groups/index'));

    

}


	public function del(){
		$id=I('get.id');
		$this->model=M('groups');
		//$this->model->del($id);
		//M('user')->del(I('get.id'))
		$result=$this->model->delete($id);
		if ($result) {
			$this->success('删除成功',U('groups/index'));
			# code...
		}
		else{
			$this->error('删除失败');

		}
      /*  parent::del($this->tbl);*/

	}
}